#ifndef __TOUCH_H
#define __TOUCH_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input.h>
#include <sys/select.h>
#include <sys/time.h>

typedef enum
{
    NONE,
    UP,
    DOWN,
    LEFT,
    RIGHT
} TouchDirection;

typedef enum
{
    NONE_CLICK,   // 无点击
    SINGLE_CLICK, // 单击
    DOUBLE_CLICK, // 双击
    SLIDE         // 滑动
} TouchType;

/// @brief  初始化触摸屏
/// @return FILE
static inline FILE *touch_init()
{
    FILE *fp_touch = fopen("/dev/input/event0", "r");
    if (fp_touch == NULL)
    {
        perror("touch_fopen");
    }
    else
    {
        printf("open touch success\n");
    }

    return fp_touch;
}

/// @brief  获取触摸屏手势和触摸事件
/// @param  fp_touch   // 触摸屏文件
/// @param  direction  // 手势方向
/// @param  touch_type // 触摸事件
static inline void get_touch_gesture(FILE *fp_touch, int *direction, int *touch_type)
{
    int sx = -1, sy = -1, ex = -1, ey = -1;
    struct input_event buf;
    int isPressDown = 0; // 是否按下

    while (1)
    {
        memset(&buf, 0, sizeof(buf));

        // 非阻塞读取
        if (fread(&buf, sizeof(buf), 1, fp_touch) != 1)
        {
            *direction = NONE;
        }

        // 坐标开始变化
        if (buf.type == EV_ABS)
        {
            if (isPressDown == 0)
            {
                if (buf.code == ABS_X)
                {
                    sx = buf.value * 800 / 1024;
                }
                if (buf.code == ABS_Y)
                {
                    sy = buf.value * 480 / 600;
                }
            }
            else
            {
                if (buf.code == ABS_X)
                {
                    ex = buf.value * 800 / 1024;
                }
                if (buf.code == ABS_Y)
                {
                    ey = buf.value * 480 / 600;
                }
            }
        }

        // 触摸动作开始（手指按下） 或触摸动作结束（手指松开）
        if (buf.type == EV_KEY && buf.code == BTN_TOUCH)
        {
            if (buf.value == 1)
            { // 按下
                isPressDown = 1;
                if (sx > 0 && sx < 200)
                {
                    *touch_type = SINGLE_CLICK;
                    *direction = LEFT;
                    return;
                }
                if (sx > 600 && sx < 800)
                {
                    *touch_type = SINGLE_CLICK;
                    *direction = RIGHT;
                    return;
                }
            }
            else
            { // 松开
                isPressDown = 0;

                if (ex != -1 && ey != -1 && abs(sx - ex) > 100)
                {
                    *touch_type = SLIDE;
                    *direction = sx > ex ? LEFT : RIGHT;
                }
                else if (sy != -1 && ey != -1 && abs(sy - ey) > 100)
                {
                    *touch_type = SLIDE;
                    *direction = sy > ey ? UP : DOWN;
                }
                else
                {
                    *touch_type = SLIDE;
                    *direction = NONE;
                }
                return;
            }
        }
    }
}

#endif